/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aiaudio.processing.base.helpers.latex.tables;

import aiaudio.processing.base.helpers.latex.ITexableTable;
import java.io.IOException;
import java.util.Vector;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;

/**
 *
 * @author nastya
 */
public class FullHTableForTex implements ITexableTable {

    private HTable table;
    private final Vector<ColumnAddress> columns;
    private final String tableCaption;
    private long version;
    private boolean showRowKey;

    public FullHTableForTex(HTable table, Vector<ColumnAddress> columns, String tableCaption, long version) {
        this.table = table;
        this.columns = columns;
        this.tableCaption = tableCaption;
        this.version = version;
    }

    public FullHTableForTex(HTable table, Vector<ColumnAddress> columns, String tableCaption) {
        this(table, columns, tableCaption, -1);
    }

    public boolean isShowRowKey() {
        return showRowKey;
    }

    public void setShowRowKey(boolean showRowKey) {
        this.showRowKey = showRowKey;
    }

    public long getVersion() {
        return version;
    }

    public void setVersion(long version) {
        this.version = version;
    }

    @Override
    public int getColumnCount() {
        return showRowKey ? columns.size() + 1 : columns.size();
    }

    @Override
    public String getColumn(int i) {
        ColumnAddress a;
        if (showRowKey) {
            if (i == 0) {
                return "key";
            }
            a = columns.get(i - 1);
        } else {
            a = columns.get(i);
        }
        return a.getFamilyStringRepresentation() + ":" + a.getColumnStringRepresentation();
    }
    private ResultScanner scanner = null;
    private Result currentResult = null;

    @Override
    public void reset() {
        close();
    }

    @Override
    public void start() throws IOException {
        Scan scan = new Scan();
        if (version >= 0) {
            scan.setTimeStamp(version);
        }
        scanner = table.getScanner(scan);
    }

    @Override
    public boolean moveNext() throws IOException {
        currentResult = scanner.next();
        return currentResult != null;
    }

    @Override
    public String getCurrentRowValue(int columnIndex) {
        ColumnAddress c;
        if (showRowKey) {
            if (columnIndex == 0) {
                return ColumnAddress.converterString.convert(currentResult.getRow());
            }
            c = columns.get(columnIndex - 1);
        } else {
            c = columns.get(columnIndex);
        }
        KeyValue columnLatest = currentResult.getColumnLatest(c.getFamily(), c.getColumn());
        if (columnLatest == null) {
            return "";
        }


        return c.convertValue(columnLatest.getValue());
    }

    @Override
    public String getName() {
        if (version < 0) {
            return tableCaption;
        }

        return tableCaption + ":" + version;
    }

    @Override
    public void close() {
        if(scanner != null){
            scanner.close();
            scanner = null;
        }
        currentResult = null;
    }
    
}
